package cn.kooki.app.duobao.util.manager;

import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;

import com.socks.library.KLog;

import java.util.ArrayList;

import cn.kooki.app.duobao.data.Bean.Index.GoodsItem;
import cn.kooki.app.duobao.data.Bean.Result.ResultItem;
import cn.kooki.app.duobao.util.StringUtils;

/**
 * Created by xiong on 2015/11/28.
 */
public class CountDownTimerManager {

    private static final int MSG_RUN = 1;

    private static final int MSG_PAUSE = 2;
    private static final int countDownInterval = 10;

    private static final int defaultTime = 8 * 60 * 1000;

    boolean iscanstart = false;
    private int zerocount = 0;

    private int min = 0;
    private int second = 0;
    private int msecode = 0;

    private ArrayList<ResultCounTDownTimer> timers = new ArrayList<>();
    StringBuilder sb;


    private static CountDownTimerManager countDownTimerManager;
    public CountDownObservable CountDownobservable;
    private ArrayList<GoodsItem> datas = new ArrayList<>();

    private CountDownTimerManager() {
        CountDownobservable = new CountDownObservable();
    }

    public static CountDownTimerManager getInstance() {
        if (countDownTimerManager == null) {
            countDownTimerManager = new CountDownTimerManager();
        }
        return countDownTimerManager;
    }

    public void addData(final GoodsItem item) {
        boolean canadd = true;
        for (int i = 0; i < datas.size(); i++) {
            GoodsItem mitem = datas.get(i);
            if (mitem.getId().equals(item.getId()) && mitem.getQishu().equals(item.getQishu())) {
                canadd = false;
            }
        }
        if (canadd) {
            if (TextUtils.isEmpty(item.getQ_uid()) && StringUtils.nullSafeParseInt(item.getStatus()) == 1) {
                ResultCounTDownTimer timer = new ResultCounTDownTimer(item.getNew_time(), countDownInterval);

                timer.setCallback(new ResultCounTDownTimer.TimerCallback() {
                    @Override
                    public void notice(long millisUntilFinished) {
                        item.setNew_time((int) millisUntilFinished);
                        CountDownobservable.notifyChanged(item);
                    }

                    @Override
                    public void finish() {
                        item.setNew_time(0);
                        CountDownobservable.notifyChanged(item);
                    }
                });
                timers.add(timer);
                timer.start();
                iscanstart = true;
            }
        }
    }

    public void setDatas(ArrayList<GoodsItem> datas) {
        this.datas.clear();
        clearTimers();
        this.datas.addAll(datas);
        for (int i = 0; i < this.datas.size(); i++) {
            final GoodsItem item = datas.get(i);
            if (StringUtils.isEquals(item.getStatus(),"1")) {
                ResultCounTDownTimer timer = new ResultCounTDownTimer(item.getNew_time(), countDownInterval);

                timer.setCallback(new ResultCounTDownTimer.TimerCallback() {
                    @Override
                    public void notice(long millisUntilFinished) {
                        item.setNew_time((int) millisUntilFinished);
                        CountDownobservable.notifyChanged(item);
                    }

                    @Override
                    public void finish() {
                        item.setNew_time(0);
                        CountDownobservable.notifyChanged(item);
                    }
                });
                timers.add(timer);
                iscanstart = true;
            }
        }
    }


    public synchronized void startCountDown() {
        for (int i = 0; i < timers.size(); i++) {
            ResultCounTDownTimer timer = timers.get(i);
            timer.start();
        }
    }

    public synchronized void stopCountDown() {
        for (int i = 0; i < timers.size(); i++) {
            ResultCounTDownTimer timer = timers.get(i);
            timer.cancel();
        }
    }

    public synchronized void clearTimers() {
        stopCountDown();
        timers.clear();
    }


    public String transtime(int time) {
        StringBuilder sb = new StringBuilder();
        long min = time / 60000;
        long second = time % 60000 / 1000;
        long mscond = time % 60000 % 1000 / 10;
        if (min < 10) {
            sb.append(0);
        }
        sb.append(min);
        sb.append(":");
        if (second < 10) {
            sb.append(0);
        }
        sb.append(second);
        sb.append(":");
        if (mscond < 10) {
            sb.append(0);
        }
        sb.append(mscond);
        String log = sb.toString();
        KLog.i(log);
        return log;
    }
}
